<template>
    <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'
import {orderStatistics} from '@/api/modules/common'

require('echarts/theme/macarons') // echarts theme
const animationDuration = 6000

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '400px'
        }
    },
    data() {
        return {
            chart: null

        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        async initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            const res = await orderStatistics()
            const array1 = []
            const array2 = []
            for (let i = 0; i < 12; i++) {
                array1.push(res.data[0][i + 1])
                array2.push(res.data[1][i + 1])
            }
            console.log(array1)
            console.log(array2)

            // if (res.data[0] !== null) {
            //   res.data.map(item => {
            //     // console.log(item)
            //     for (const i in item) {
            //       const limits = ['year', 'branchMoney']
            //       if (!limits.includes(i)) {
            //         this.data1.push(item[i])
            //       }
            //     }
            //     console.log(this.data1)
            //   })
            // }
            this.chart.setOption({
                // title: {
                //   text: '订单金额统计',
                //   top: 0,
                //   left: 'center'
                // },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: { // 坐标轴指示器，坐标轴触发有效
                        type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
                    }
                },
                // legend: {
                //   data: ['普通订单', '合伙人缴费']
                // },
                grid: {
                    top: 10,
                    left: '2%',
                    right: '2%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: [{
                    type: 'category',
                    data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
                    axisTick: {
                        alignWithLabel: true
                    }
                }],
                yAxis: [{
                    type: 'value',
                    axisTick: {
                        show: false
                    }
                }],
                series: [{
                    name: '普通订单',
                    type: 'bar',
                    stack: 'vistors',
                    barWidth: '60%',
                    data: array1,
                    animationDuration
                }, {
                    name: '合伙人缴费',
                    type: 'bar',
                    stack: 'vistors',
                    barWidth: '60%',
                    data: array2,
                    animationDuration
                }
                ]
            })
        }
    }
}
</script>
